/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.magicland;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class PlayerManager {

        private static Player pl;
        private final String fileName = "player_info.txt";
        
        
        
        
        public PlayerManager() {
                 
            if(pl == null){
                pl = new Player();

                try {
                        loadPlayer();
                } catch (IOException e) {

                        e.printStackTrace();
                }
            }
        }
        
       
        public Player loadPlayer() throws IOException{
                
                FileReader fr = new FileReader(Config.path + fileName);
                BufferedReader br = new BufferedReader(fr);
               
                                
//                private String nome;       
//                private int tutorialComplete;
//                private int ouro;
//                private int energia;
//                private int fruta;
//                private int madeira;
//                private int nivel; 
                
                
                //nome
                pl.setNome(br.readLine());
                
                //tutorial
                pl.setTutorialComplete(Integer.valueOf(br.readLine()));
                
                //ouro
                pl.setOuro(Integer.valueOf(br.readLine()));
                
                //energia
                pl.setEnergia(Integer.valueOf(br.readLine()));
                
                //fruta
                pl.setFruta(Integer.valueOf(br.readLine()));
                
                //madeira
                pl.setMadeira(Integer.valueOf(br.readLine()));
                
                
                pl.setDragonDiscovered(Integer.valueOf(br.readLine()));
//                //nivel
//                pl.setNivel(Integer.valueOf(br.readLine()));

                br.close();
                
                return pl;

        }

        public Player removeSave(){
            //createDefaultPlayer
          //  Player pl = new Player();


            pl.setNome( "" );

            //tutorial
            pl.setTutorialComplete(0);

            //ouro
            pl.setOuro( 5 );

            //energia
            pl.setEnergia( 5 );

            //fruta
            pl.setFruta( 5 );

            //madeira
            pl.setMadeira( 5 );


            pl.setDragonDiscovered( 0 );

            
            savePlayer( pl );
            return pl;
        }
        
        public void savePlayer(Player pl) //throws IOException
        {
        try{
                FileWriter fw = new FileWriter(Config.path + fileName, false);
                BufferedWriter bw = new BufferedWriter(fw);
        
                
//                private String nome;       
//                private int tutorialComplete;
//                private int ouro;
//                private int energia;
//                private int fruta;
//                private int madeira;
//                private int nivel; 
        
                System.out.println(pl);
                //nome
                bw.write(pl.getNome());
                bw.newLine();
 
                bw.write( String.valueOf( pl.getTutorialComplete() ));
                bw.newLine();
                //ouro
                bw.write( String.valueOf( pl.getOuro() ));
                bw.newLine();
                //energia
                bw.write( String.valueOf( pl.getEnergia() ));
                bw.newLine();
                //fruta
                bw.write( String.valueOf(  pl.getFruta()) );
                bw.newLine();
                //madeira
                bw.write( String.valueOf(  pl.getMadeira()) );
                bw.newLine();
                
                bw.write( String.valueOf(  pl.getDragonDiscovered()) );
                
                
                //nivel
               // bw.write(pl.getNivel());
        
                bw.close();
        }catch(Exception e){
            e.printStackTrace();
        }

}

        public Player getPlayer(){
                return pl;
        }
        

}
